package com.gzz.manage.base.customer;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.gzz.common.base.BaseDao;
import com.gzz.common.base.Page;

import lombok.extern.slf4j.Slf4j;

/**
 * @类说明 【客户信息】数据访问层
 * @author 高振中
 * @date 2021-03-21 11:03:35
 **/
@Slf4j
@Repository
public class CustomerDao extends BaseDao {

	/**
	 * @方法说明 【客户信息】新增
	 */
	public int save(Customer vo) {
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO base_customer (id,username,password,wechat,address,avatar,remark,tel,levelId,createTime,updateTime)");
		sql.append(" VALUES (?,?,?,?,?,?,?,?,?,?,?)");
		Object[] param = { vo.getId(), vo.getUsername(), vo.getPassword(), vo.getWechat(), vo.getAddress(), vo.getAvatar(), vo.getRemark(), vo.getTel(), vo.getLevelId(), vo.getCreateTime(), vo.getUpdateTime() };
		// log.info(sql(sql.toString(), param));// 显示SQL语句
		return update(sql.toString(), param);
	}

	/**
	 * @方法说明 【客户信息】删除
	 */
	public int delete(Object[] ids) {
		String sql = "DELETE FROM base_customer WHERE id IN" + toIn(ids);
		// log.info(sql(sql, ids));// 显示SQL语句
		return update(sql, ids);
	}

	/**
	 * @方法说明 【客户信息】修改
	 */
	public int update(Customer vo) {
		StringBuilder sql = new StringBuilder();
		sql.append("UPDATE base_customer SET username=?,password=?,wechat=?,address=?,avatar=?,remark=?,tel=?,levelId=?,createTime=?,updateTime=?");
		sql.append(" WHERE id=? ");
		Object[] param = { vo.getUsername(), vo.getPassword(), vo.getWechat(), vo.getAddress(), vo.getAvatar(), vo.getRemark(), vo.getTel(), vo.getLevelId(), vo.getCreateTime(), vo.getUpdateTime(), vo.getId() };
		// log.info(sql(sql.toString(), param));// 显示SQL语句
		return update(sql.toString(), param);
	}

	/**
	 * @方法说明 【客户信息】分页列表
	 */
	public Page<Customer> page(CustomerCond cond) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t.id,t.username,t.password,t.wechat,t.address,t.avatar,t.remark,t.tel,t.levelId,t.createTime,t.updateTime");
		sql.append(" FROM base_customer t");
		sql.append(cond.where());
		log.info(sql(sql.toString(), cond.array()));// 显示SQL语句
		return page(sql.toString(), cond, Customer.class);
	}

	/**
	 * @方法说明 【客户信息】列表
	 */
	public List<Customer> list(CustomerCond cond) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t.id,t.username,t.password,t.wechat,t.address,t.avatar,t.remark,t.tel,t.levelId,t.createTime,t.updateTime");
		sql.append(" FROM base_customer t");
		sql.append(cond.where());
		sql.append(" ORDER BY id DESC");
		// log.info(sql(sql.toString(),cond.array()));//显示SQL语句
		return list(sql.toString(), cond, Customer.class);
	}

	/**
	 * @方法说明 【客户信息】按主键查
	 */
	public Customer findById(Integer id) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT t.id,t.username,t.password,t.wechat,t.address,t.avatar,t.remark,t.tel,t.levelId,t.createTime,t.updateTime");
		sql.append(" FROM base_customer t WHERE t.id=?");
		// log.info(sql(sql.toString(),id));//显示SQL语句
		return findById(sql.toString(), Customer.class, id);
	}

	/**
	 * @方法说明 【客户信息】个数
	 */
	public int count(CustomerCond cond) {
		String sql = "SELECT COUNT(1) FROM base_customer t " + cond.where();
		// log.info(sql(sql,cond.array()));//显示SQL语句
		return count(sql, cond);
	}

	/**
	 * @方法说明 【客户信息】新增并返回自增涨主键
	 */
	public int saveReturnPK(Customer vo) {
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO base_customer (id,username,password,wechat,address,avatar,remark,tel,levelId,createTime,updateTime)");
		sql.append(" VALUES (:id,:username,:password,:wechat,:address,:avatar,:remark,:tel,:levelId,:createTime,:updateTime)");
		// log.info(sqlp(sql.toString(), vo));// 显示SQL语句
		return saveKey(vo, sql.toString(), "id");
	}

	/**
	 * @方法说明 【客户信息】批量插入
	 */
	public int[] insertBatch(List<Customer> list) {
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT INTO base_customer (id,username,password,wechat,address,avatar,remark,tel,levelId,createTime,updateTime)");
		sql.append(" VALUES (:id,:username,:password,:wechat,:address,:avatar,:remark,:tel,:levelId,:createTime,:updateTime)");
		// log.info(sqlp(sql.toString(), list));// 显示SQL语句
		return batchOperate(list, sql.toString());
	}
	/**
	 * @方法说明 【客户信息】批量更新
	 */
	public int[] updateBatch(List<Customer> list) {
		StringBuilder sql = new StringBuilder();
		sql.append("UPDATE base_customer SET id=:id,username=:username,password=:password,wechat=:wechat,address=:address,avatar=:avatar,remark=:remark,tel=:tel,levelId=:levelId,createTime=:createTime,updateTime=:updateTime");
		sql.append(" WHERE id=:id ");
		// log.info(sqlp(sql.toString(), list));// 显示SQL语句
		return batchOperate(list, sql.toString());
	}
	/**
	 * @方法说明 【客户信息】逻辑删除
	 */
//	public int delete(Object... ids) {
//		String sql = "UPDATE base_customer SET dr=1 WHERE id IN " + toIn(ids);
//		return update(sql,ids);
//	}	

}